﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Globalization;
using Libs.HR;

public partial class Pages_Hr_Family_Edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AppUtils.CheckLogin();
        Title = Title + " - Cập nhật quan hệ gia đình";
        if (!IsPostBack)
        {
            btDelete.Attributes.Add("onclick", "return confirm('Bạn có đồng ý xóa?');");
            init();
        }
    }

    private void init()
    {
        // Loại quan hệ
        var _FamilyType = new FamilyType();
        drpFamilyType.DataSource = _FamilyType.GetTList();
        drpFamilyType.DataBind();
        drpFamilyType.Items.Insert(0, new ListItem("Chọn quan hệ:", "0"));

        var _Family = new Family() { FamilyID = AppUtils.Request("id") };
        _Family = _Family.Get();
        if (_Family == null) Response.Redirect(Constant.ADMIN_PATH + Resources.Url.EmployeesList);

        txtFirstName.Text = _Family.FirstName;
        txtLastName.Text = _Family.LastName;
        txtBirthDay.Text = _Family.BirthDay;
        txtAddress.Text = _Family.Address;
        txtDescription.Text = _Family.Description;
        drpFamilyType.SelectedValue = _Family.TypeID.ToString();

        var _Employee = new Employees() { EmployeeID = _Family.EmployeeID };
        _Employee = _Employee.Get();
        if (_Employee == null) Response.Redirect(Constant.ADMIN_PATH + Resources.Url.EmployeesList);

        lblEmployee.Text = _Employee.LastName + " " + _Employee.FirstName;
    }

    protected void btUpdate_Click(object sender, EventArgs e)
    {
        var _Family = new Family() { FamilyID = AppUtils.Request("id") };
        _Family = _Family.Get();
        _Family.FirstName = txtFirstName.Text;
        _Family.LastName = txtLastName.Text;
        _Family.TypeID = Convert.ToInt32(drpFamilyType.SelectedValue);
        _Family.BirthDay = txtBirthDay.Text.Trim();
        _Family.Address = txtAddress.Text.Trim();
        _Family.Description = txtDescription.Text.Trim(); 
        _Family.Update();

        Response.Redirect(Constant.ADMIN_PATH + Resources.Url.EmployeesEdit + "?id=" + _Family.EmployeeID.ToString());
    }

    protected void btDelete_Click(object sender, EventArgs e)
    {
        var _Family = new Family() { FamilyID = AppUtils.Request("id") };
        _Family.Delete();

        Response.Redirect(Constant.ADMIN_PATH + Resources.Url.EmployeesEdit + "?id=" + _Family.EmployeeID.ToString());
    }

    protected void btCancel_Click(object sender, EventArgs e)
    {
        var _Family = new Family() { FamilyID = AppUtils.Request("id") };
        _Family = _Family.Get();
        Response.Redirect(Constant.ADMIN_PATH + Resources.Url.EmployeesEdit + "?id=" + _Family.EmployeeID.ToString());
    }
}
